package hamaster.jpbc.gui;

import static hamaster.jpbc.gui.Messages.getString;
import static hamaster.jpbc.userconfig.ConfigKeys.DEFAULT_ID_FILE;
import static java.awt.FlowLayout.LEFT;
import static javax.swing.BoxLayout.Y_AXIS;
import hamaster.jpbc.Main;

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

public class SignPanel extends JPanel implements ItemListener {
	private static final long serialVersionUID = 1L;

	private ChooseFilePanel chooseCertPanel;
	private JCheckBox enable;

	public SignPanel() {
		setLayout(new BoxLayout(this, Y_AXIS));
		chooseCertPanel = new ChooseFilePanel(getString("SignPanel.personalParams")); //$NON-NLS-1$
		enable = new JCheckBox(getString("SignPanel.signOrNot")); //$NON-NLS-1$
		chooseCertPanel.disableChooseFile();
		chooseCertPanel.setFileFilter(new FileNameExtensionFilter(getString("SignPanel.paramsExt"), "id")); //$NON-NLS-1$ //$NON-NLS-2$
		
		String recentCert = Main.getUserConfig().getProperty(DEFAULT_ID_FILE);
		if (recentCert != null)
			chooseCertPanel.setCurrentDirectory(new File(recentCert));
		
		enable.addItemListener(this);
		JPanel chkBx = new JPanel(new FlowLayout(LEFT));
		chkBx.add(Box.createHorizontalStrut(45));
		chkBx.add(enable);
		add(chkBx);
		add(chooseCertPanel);
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource() == enable) {
			if (enable.isSelected()) {
				chooseCertPanel.enableChooseFile();
			} else {
				chooseCertPanel.disableChooseFile();
			}
		}
	}

	/**
	 * @return 个人参数的路径
	 */
	public String getCertPath() {
		return chooseCertPanel.getSelectedFilePath();
	}

	/**
	 * 是否启用了签名
	 * @return 启用签名返回true
	 */
	public boolean signEnabled() {
		return enable.isSelected();
	}

}
